import 'dart:async';

import 'package:asperm/tools/const.dart';
import 'package:asperm/tools/widget_extension.dart';
import 'package:asperm/views/custom_cell.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:image_picker/image_picker.dart';

import '../pages/testing_steps_page.dart';
import '../tools/configuration.dart';
import '../tools/motility.dart';
import 'myDialog.dart';
import 'steps_widget.dart';

typedef ClickActionCallback = void Function();

class Steps_Widget extends StatefulWidget {
   Steps_Widget({
    super.key,
    required this.nextAction,
    required this.backAction,
    required this.steps,
  });

  final ClickActionCallback nextAction;
  final ClickActionCallback backAction;
  Testing_steps steps;

  @override
  State<Steps_Widget> createState() => _Steps_WidgetState();
}

class _Steps_WidgetState extends State<Steps_Widget> with SingleTickerProviderStateMixin,WidgetsBindingObserver{

  int currentStep = 0;

  int step_2_1_value1 = 0;
  int step_2_1_value2 = 0;

  Timer? _timer;
  int _liquify_totalSeconds = 1 * 10;
  int _liquify_remainingSeconds = 1 * 10; // 十分钟，即 600 秒
  bool _liquify_isFinish = false;


  final int _testing_totalSeconds = 1 * 60;
  int _testing_remainingSeconds = 1 * 60;
  bool _testing_isFinish = false;

  DateTime? startTime;//记录开始倒计时的时间
  final ImagePicker _picker = ImagePicker();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    print('-----------initState');

    updateCurrentSteps();
    WidgetsBinding.instance.addObserver(this);

  }

  @override
  void dispose() {

    WidgetsBinding.instance.removeObserver(this);

   if(widget.steps == Testing_steps.steps_liquify_countDown && _timer != null){
       _timer!.cancel();
   }
    super.dispose();
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);
    switch (state) {
      case AppLifecycleState.resumed:
      // 应用回到前台，恢复动画
        print('----------应用回到前台，恢复动画');
        if((widget.steps == Testing_steps.steps_liquify_countDown && _liquify_isFinish != true) || (widget.steps == Testing_steps.steps_testing_in_process && _testing_isFinish != true)) {

        }else{
          return;
        }


        if( startTime != null && (widget.steps == Testing_steps.steps_liquify_countDown || widget.steps == Testing_steps.steps_testing_in_process)){
          print('---------这里');
          DateTime endTime = DateTime.now();
          Duration difference = endTime.difference(startTime!);
          // 获取相差的秒数
          int secondsDifference = difference.inSeconds;
          print('---------secondsDifference：${secondsDifference}');
          int totalSeconds = 0;
          if(widget.steps == Testing_steps.steps_liquify_countDown){
            totalSeconds = _liquify_totalSeconds;
          }else{
            totalSeconds = _testing_totalSeconds;
          }

          if(secondsDifference >= totalSeconds){
            print('---------这里 11');
            startTime = null;
            if(_timer != null){
              _timer!.cancel();
            }

            if(widget.steps == Testing_steps.steps_liquify_countDown) {
              _liquify_isFinish = true;
            }else{
              _testing_isFinish = true;
            }
              setState(() {
              if(widget.steps == Testing_steps.steps_liquify_countDown){
                currentStep = 3;
                widget.steps = Testing_steps.steps_4;
              }else{

              }
            });
            widget.nextAction();
          }else{

            setState(() {
              if(widget.steps == Testing_steps.steps_liquify_countDown){
                _liquify_remainingSeconds = _liquify_totalSeconds  - secondsDifference;

              }else{
                _testing_remainingSeconds = _testing_totalSeconds - secondsDifference;
              }
            });
            _startTimer();
          }
        }
        break;
      case AppLifecycleState.paused:
      // 应用进入后台，暂停动画
      //   _controller.stop();
        if(_timer != null){
          _timer!.cancel();
        }
        break;
      default:
        break;
    }
  }



  updateCurrentSteps(){
    switch (widget.steps) {
      case Testing_steps.steps_1: {
        currentStep = 0;
      }
      break;
      case Testing_steps.steps_2_1:
      case Testing_steps.steps_2_2: {
        currentStep = 1;
      }
      break;
      case Testing_steps.steps_3: {
        currentStep = 2;
      }
      break;
      case Testing_steps.steps_4: {
        currentStep = 3;
      }
      break;
      case Testing_steps.steps_5: {
        currentStep = 4;
      }
      break;
      default: {
        currentStep = 0;
      }
    }
  }

  @override
  void didUpdateWidget(covariant Steps_Widget oldWidget) {
    // TODO: implement didUpdateWidget
    super.didUpdateWidget(oldWidget);
    setState(() {
      updateCurrentSteps();
    });
  }




  @override
  Widget build(BuildContext context) {

    print('-------build');
    return stepsWidget(context);
  }

  stepsWidget(BuildContext context){

    print('-----widget.steps:${widget.steps}');

    switch (widget.steps) {
      case Testing_steps.steps_1: {
        return steps_1_widget(context);
      }
      case Testing_steps.steps_2_1: {
        return steps_2_1_widget(context);
      }
      case Testing_steps.steps_2_2: {
        return steps_2_2_widget(context);
      }
      case Testing_steps.steps_3: {
        return steps_3_widget(context);
      }
      case Testing_steps.steps_liquify_countDown: {

        print('-----------startTime');
        if(_timer == null){
          startTime = DateTime.now();
          _startTimer();
        }
        return liquify_countDown_widget(context);
      }
      case Testing_steps.steps_4: {
        return steps_4_widget(context);
      }
      case Testing_steps.steps_5: {
        return steps_5_widget(context);
      }
      case Testing_steps.steps_testing_in_process:{
        print('-----------startTime');

        if(_timer == null){
          startTime = DateTime.now();
          _startTimer();
        }

        return testing_in_process_widget(context);
      }
      default: {
        return steps_1_widget(context);
      }
    }

  }



  steps_1_widget(BuildContext context) {
    return Scrollbar(
        thickness: 6,
        radius: const Radius.circular(3),
        interactive: true,
        child: SingleChildScrollView(

          child: Container(
            margin: EdgeInsets.only(left: 20.w,right: 20.w),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                CustomStepIndicator(
                  currentStep: currentStep,
                ),
                Text(
                  'CONNECT DEVICE POWER',
                  style: TextStyle(
                    color: Colors.black,
                    fontSize: 20.sp,
                    fontWeight: FontWeight.w600,
                    letterSpacing: 0,
                    height: 1.2,
                  ),
                ),
                SizedBox(height: 40.h,),
                Text(
                  'Pour one vial of the liquefying powder into the collected sample.',
                  style: TextStyle(
                    color: Colors.black,
                    fontSize: 16.sp,
                    fontWeight: FontWeight.w600,
                    letterSpacing: 0,
                    height: 1.2,
                  ),
                ),
                SizedBox(height: 20.h,),
                Container(
                  alignment: Alignment.center,
                  child:  Image.asset('images/device_power.png',height: 250.w,width: 250.w,),
                ),

                Container(
                  height: 48.h,
                  width: 335.w,
                  decoration: BoxDecoration(
                    color: Color.fromRGBO(16, 37, 105, 1),
                    borderRadius: BorderRadius.circular(5.h),
                  ),
                  margin: EdgeInsets.only(top: 60.h),
                  child: TextButton(
                    onPressed: () {
                      widget.nextAction();
                      },
                    child: Text(
                      'NEXT',
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 16.sp,
                        fontWeight: FontWeight.w400,
                      ),
                    ),
                  ),
                ),
                Container(
                  height: 48.h,
                  width: 335.w,
                  decoration: BoxDecoration(
                    color: Color.fromRGBO(16, 37, 105, 1),
                    borderRadius: BorderRadius.circular(5.h),
                  ),
                  margin: EdgeInsets.only(top: 20.h),
                  child: TextButton(
                    onPressed: () {
                      widget.backAction();
                    },
                    child: Text(
                      'BACK',
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 16.sp,
                        fontWeight: FontWeight.w400,
                      ),
                    ),
                  ),
                ),

              ],
            ),
          )
        )
    );
  }

  steps_2_1_widget(BuildContext context) {
    return Scrollbar(
        thickness: 6,
        radius: const Radius.circular(3),
        interactive: true,
        child: SingleChildScrollView(

            child: Container(
              margin: EdgeInsets.only(left: 20.w,right: 20.w),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  CustomStepIndicator(
                    currentStep: currentStep,
                  ),
                  Text(
                    'COLLECT YOUR SAMPLE',
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: 20.sp,
                      fontWeight: FontWeight.w600,
                      letterSpacing: 0,
                      height: 1.2,
                    ),
                  ),
                  SizedBox(height: 40.h,),
                  Text(
                    'Read the Product instructions before starting',
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: 16.sp,
                      fontWeight: FontWeight.w600,
                      letterSpacing: 0,
                      height: 1.2,
                    ),
                  ),
                  SizedBox(height: 20.h,),
                  Text(
                    'Collect your entire sample in the container provided. Please DO NOT use ANY lubricant, cream or partners DO NOT use condoms',
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: 16.sp,
                      fontWeight: FontWeight.w600,
                      letterSpacing: 0,
                      height: 1.2,
                    ),
                  ),
                  SizedBox(height: 20.h,),
                  Row(
                    children: [
                      Container(
                        alignment: Alignment.center,
                        child:  Image.asset('images/door.png',height: 154.w,width: 199.w,),
                      ),
                      Container(
                        alignment: Alignment.center,
                        child:  Image.asset('images/no_sanitiser.png',height: 100.w,width: 100.w,),
                      ),
                    ],
                  ),
                  Container(
                    height: 48.h,
                    width: 335.w,
                    decoration: BoxDecoration(
                      color: Color.fromRGBO(16, 37, 105, 1),
                      borderRadius: BorderRadius.circular(5.h),
                    ),
                    margin: EdgeInsets.only(top: 60.h),
                    child: TextButton(
                      onPressed: () {
                        widget.nextAction();
                      },
                      child: Text(
                        'NEXT',
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: 16.sp,
                          fontWeight: FontWeight.w400,
                        ),
                      ),
                    ),
                  ),
                  Container(
                    height: 48.h,
                    width: 335.w,
                    decoration: BoxDecoration(
                      color: Color.fromRGBO(16, 37, 105, 1),
                      borderRadius: BorderRadius.circular(5.h),
                    ),
                    margin: EdgeInsets.only(top: 20.h),
                    child: TextButton(
                      onPressed: () {
                        widget.backAction();
                      },
                      child: Text(
                        'BACK',
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: 16.sp,
                          fontWeight: FontWeight.w400,
                        ),
                      ),
                    ),
                  ),

                ],
              ),
            )
        )
    );
  }

  steps_2_2_widget(BuildContext context) {
    return Scrollbar(
        thickness: 6,
        radius: const Radius.circular(3),
        interactive: true,
        child: SingleChildScrollView(

            child: Container(
              margin: EdgeInsets.only(left: 20.w,right: 20.w),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  CustomStepIndicator(
                    currentStep: currentStep,
                  ),
                  Text(
                    'Liquid Level',
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: 20.sp,
                      fontWeight: FontWeight.w600,
                      letterSpacing: 0,
                      height: 1.2,
                    ),
                  ),
                  SizedBox(height: 20.h,),
                  Row(
                    children: [
                      Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Container(
                            child: Row(
                              children: [
                                SizedBox(
                                  width: 30.w,
                                  height: 30.w,
                                  child: Checkbox(
                                      activeColor:globalColor(),
                                      checkColor: globalColor(),
                                      value: step_2_1_value1 == 0 ? true :false,
                                      shape: CircleBorder(),
                                      onChanged: (value){
                                        setState(() {
                                          step_2_1_value1 = 0;
                                        });
                                      }
                                  ),
                                ),
                                SizedBox(width: 5.w,),
                                Text(
                                  'Above the mark',
                                  textAlign: TextAlign.start,
                                  style: TextStyle(
                                    color: Colors.black,
                                    fontSize: 14.sp,
                                    fontWeight: FontWeight.w400,
                                    letterSpacing: 0,
                                    height: 1.2,
                                  ),
                                )
                              ],
                              mainAxisAlignment: MainAxisAlignment.start,
                            ),
                          ).gestureDetector(onTap: (){
                            setState(() {
                              step_2_1_value1 = 0;
                            });
                          }),
                          Container(
                            child: Row(
                              children: [
                                SizedBox(
                                  width: 30.w,
                                  height: 30.w,
                                  child: Checkbox(
                                      activeColor:globalColor(),
                                      checkColor: globalColor(),
                                      value: step_2_1_value1 == 1 ? true :false,
                                      shape: CircleBorder(),
                                      onChanged: (value){
                                        setState(() {
                                          step_2_1_value1 = 1;
                                        });
                                      }
                                  ),
                                ),
                                SizedBox(width: 5.w,),
                                Text(
                                  'Equal height to the mark',
                                  textAlign: TextAlign.start,
                                  style: TextStyle(
                                    color: Colors.black,
                                    fontSize: 14.sp,
                                    fontWeight: FontWeight.w400,
                                    letterSpacing: 0,
                                    height: 1.2,
                                  ),
                                )
                              ],
                              mainAxisAlignment: MainAxisAlignment.start,
                            ),
                          ).gestureDetector(onTap: (){
                            setState(() {
                              step_2_1_value1 = 1;
                            });
                          }),
                          Container(
                            child: Row(
                              children: [
                                SizedBox(
                                  width: 30.w,
                                  height: 30.w,
                                  child: Checkbox(
                                      activeColor:globalColor(),
                                      checkColor: globalColor(),
                                      value: step_2_1_value1 == 2 ? true :false,
                                      shape: CircleBorder(),
                                      onChanged: (value){
                                        setState(() {
                                          step_2_1_value1 = 2;
                                        });
                                      }
                                  ),
                                ),
                                SizedBox(width: 5.w,),
                                Text(
                                  'Below the mark',
                                  textAlign: TextAlign.start,
                                  style: TextStyle(
                                    color: Colors.black,
                                    fontSize: 14.sp,
                                    fontWeight: FontWeight.w400,
                                    letterSpacing: 0,
                                    height: 1.2,
                                  ),
                                )
                              ],
                              mainAxisAlignment: MainAxisAlignment.start,
                            ),
                          ).gestureDetector(onTap: (){
                            setState(() {
                              step_2_1_value1 = 2;
                            });
                          }),
                          ],
                      ),
                      Image.asset('images/empty_cup_l.png',height: 100.w,width: 100.w,)
                    ],
                  ),
                  Container(
                    margin: EdgeInsets.only(top: 20.h,bottom: 20.h),
                    child: Divider(
                      color: Colors.black,
                      thickness: 1,
                    ),
                  ),
                  Text(
                    'Semencolor',
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: 20.sp,
                      fontWeight: FontWeight.w600,
                      letterSpacing: 0,
                      height: 1.2,
                    ),
                  ),
                  SizedBox(height: 20.h,),
                  Row(
                    children: [
                      Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Container(
                            child: Row(
                              children: [
                                SizedBox(
                                  width: 30.w,
                                  height: 30.w,
                                  child: Checkbox(
                                      activeColor:globalColor(),
                                      checkColor: globalColor(),
                                      value: step_2_1_value2 == 0 ? true :false,
                                      shape: CircleBorder(),
                                      onChanged: (value){
                                        setState(() {
                                          step_2_1_value2 = 0;
                                        });
                                      }
                                  ),
                                ),
                                SizedBox(width: 5.w,),
                                Text(
                                  'Pale yellow or white',
                                  textAlign: TextAlign.start,
                                  style: TextStyle(
                                    color: Colors.black,
                                    fontSize: 14.sp,
                                    fontWeight: FontWeight.w400,
                                    letterSpacing: 0,
                                    height: 1.2,
                                  ),
                                )
                              ],
                              mainAxisAlignment: MainAxisAlignment.start,
                            ),
                          ).gestureDetector(onTap: (){
                            setState(() {
                              step_2_1_value2 = 0;
                            });
                          }),
                          Container(
                            child: Row(
                              children: [
                                SizedBox(
                                  width: 30.w,
                                  height: 30.w,
                                  child: Checkbox(
                                      activeColor:globalColor(),
                                      checkColor: globalColor(),
                                      value: step_2_1_value2 == 1 ? true :false,
                                      shape: CircleBorder(),
                                      onChanged: (value){
                                        setState(() {
                                          step_2_1_value2 = 1;
                                        });
                                      }
                                  ),
                                ),
                                SizedBox(width: 5.w,),
                                Text(
                                  'Red',
                                  textAlign: TextAlign.start,
                                  style: TextStyle(
                                    color: Colors.black,
                                    fontSize: 14.sp,
                                    fontWeight: FontWeight.w400,
                                    letterSpacing: 0,
                                    height: 1.2,
                                  ),
                                )
                              ],
                              mainAxisAlignment: MainAxisAlignment.start,
                            ),
                          ).gestureDetector(onTap: (){
                            setState(() {
                              step_2_1_value2 = 1;
                            });
                          }),
                          Container(
                            child: Row(
                              children: [
                                SizedBox(
                                  width: 30.w,
                                  height: 30.w,
                                  child: Checkbox(
                                      activeColor:globalColor(),
                                      checkColor: globalColor(),
                                      value: step_2_1_value2 == 2 ? true :false,
                                      shape: CircleBorder(),
                                      onChanged: (value){
                                        setState(() {
                                          step_2_1_value2 = 2;
                                        });
                                      }
                                  ),
                                ),
                                SizedBox(width: 5.w,),
                                Text(
                                  'Green',
                                  textAlign: TextAlign.start,
                                  style: TextStyle(
                                    color: Colors.black,
                                    fontSize: 14.sp,
                                    fontWeight: FontWeight.w400,
                                    letterSpacing: 0,
                                    height: 1.2,
                                  ),
                                )
                              ],
                              mainAxisAlignment: MainAxisAlignment.start,
                            ),
                          ).gestureDetector(onTap: (){
                            setState(() {
                              step_2_1_value2 = 2;
                            });
                          }),
                          Container(
                            child: Row(
                              children: [
                                SizedBox(
                                  width: 30.w,
                                  height: 30.w,
                                  child: Checkbox(
                                      activeColor:globalColor(),
                                      checkColor: globalColor(),
                                      value: step_2_1_value2 == 3 ? true :false,
                                      shape: CircleBorder(),
                                      onChanged: (value){
                                        setState(() {
                                          step_2_1_value2 = 3;
                                        });
                                      }
                                  ),
                                ),
                                SizedBox(width: 5.w,),
                                Text(
                                  'Else',
                                  textAlign: TextAlign.start,
                                  style: TextStyle(
                                    color: Colors.black,
                                    fontSize: 14.sp,
                                    fontWeight: FontWeight.w400,
                                    letterSpacing: 0,
                                    height: 1.2,
                                  ),
                                )
                              ],
                              mainAxisAlignment: MainAxisAlignment.start,
                            ),
                          ).gestureDetector(onTap: (){
                            setState(() {
                              step_2_1_value2 = 3;
                            });
                          }),
                        ],
                      ),
                      SizedBox(width: 40.h,),
                      Container(
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                          children: [
                            Container(
                              color: Colors.yellow,
                              width: 80.w,
                              height: 20.h,
                            ),
                            SizedBox(height: 10.h,),
                            Container(
                              color: Colors.red,
                              width: 80.w,
                              height: 20.h,
                            ),
                            SizedBox(height: 10.h,),
                            Container(
                              color: Colors.green,
                              width: 80.w,
                              height: 20.h,
                            )
                          ],
                        ),
                      )
                     ],
                  ),

                  Container(
                    height: 48.h,
                    width: 335.w,
                    decoration: BoxDecoration(
                      color: Color.fromRGBO(16, 37, 105, 1),
                      borderRadius: BorderRadius.circular(5.h),
                    ),
                    margin: EdgeInsets.only(top: 60.h),
                    child: TextButton(
                      onPressed: () {
                        widget.nextAction();
                      },
                      child: Text(
                        'NEXT',
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: 16.sp,
                          fontWeight: FontWeight.w400,
                        ),
                      ),
                    ),
                  ),
                  Container(
                    height: 48.h,
                    width: 335.w,
                    decoration: BoxDecoration(
                      color: Color.fromRGBO(16, 37, 105, 1),
                      borderRadius: BorderRadius.circular(5.h),
                    ),
                    margin: EdgeInsets.only(top: 20.h),
                    child: TextButton(
                      onPressed: () {
                        widget.backAction();
                      },
                      child: Text(
                        'BACK',
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: 16.sp,
                          fontWeight: FontWeight.w400,
                        ),
                      ),
                    ),
                  ),

                ],
              ),
            )
        )
    );
  }

  steps_3_widget(BuildContext context) {
    return Scrollbar(
        thickness: 6,
        radius: const Radius.circular(3),
        interactive: true,
        child: SingleChildScrollView(

            child: Container(
              margin: EdgeInsets.only(left: 20.w,right: 20.w),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  CustomStepIndicator(
                    currentStep: currentStep,
                  ),
                  Text(
                    'PREPARE YOUR SAMPLE',
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: 20.sp,
                      fontWeight: FontWeight.w600,
                      letterSpacing: 0,
                      height: 1.2,
                    ),
                  ),
                  SizedBox(height: 40.h,),
                  Text(
                    'Pour one vial of the liquefying powder into the collected sample.',
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: 16.sp,
                      fontWeight: FontWeight.w600,
                      letterSpacing: 0,
                      height: 1.2,
                    ),
                  ),
                  SizedBox(height: 20.h,),
                  Text(
                    'Mix the sample and powder using gentle circular rotation for 10-15 seconds',
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: 16.sp,
                      fontWeight: FontWeight.w600,
                      letterSpacing: 0,
                      height: 1.2,
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.only(top: 20.h),
                    alignment: Alignment.center,
                    child:  Image.asset('images/sample.png',height: 180.w,width: 180.w,),
                  ),
                  Container(
                    height: 48.h,
                    width: 335.w,
                    decoration: BoxDecoration(
                      color: Color.fromRGBO(16, 37, 105, 1),
                      borderRadius: BorderRadius.circular(5.h),
                    ),
                    margin: EdgeInsets.only(top: 60.h),
                    child: TextButton(
                      onPressed: () {
                        widget.nextAction();
                      },
                      child: Text(
                        'NEXT',
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: 16.sp,
                          fontWeight: FontWeight.w400,
                        ),
                      ),
                    ),
                  ),
                  Container(
                    height: 48.h,
                    width: 335.w,
                    decoration: BoxDecoration(
                      color: Color.fromRGBO(16, 37, 105, 1),
                      borderRadius: BorderRadius.circular(5.h),
                    ),
                    margin: EdgeInsets.only(top: 20.h),
                    child: TextButton(
                      onPressed: () {
                        widget.backAction();
                      },
                      child: Text(
                        'BACK',
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: 16.sp,
                          fontWeight: FontWeight.w400,
                        ),
                      ),
                    ),
                  ),

                ],
              ),
            )
        )
    );
  }

  liquify_countDown_widget(BuildContext context)  {
    return Scrollbar(
        thickness: 6,
        radius: const Radius.circular(3),
        interactive: true,
        child: SingleChildScrollView(

            child: Container(
              margin: EdgeInsets.only(left: 20.w,right: 20.w),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Text(
                    'It will take 10 minutes to liquify your sample',
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: 20.sp,
                      fontWeight: FontWeight.w600,
                      letterSpacing: 0,
                      height: 1.2,
                    ),
                  ),
                  SizedBox(height: 20.h,),
                  Container(
                    // color: globalColor(),
                    child: Text(
                      formatTime(_liquify_remainingSeconds),
                      style: TextStyle(
                        color: globalColor(),
                        fontSize: 42.sp,
                        fontWeight: FontWeight.w600,
                      ),
                    ),
                  ),
                  SizedBox(height: 20.h,),
                  Text(
                    'Time countdown',
                    style: TextStyle(
                      color: globalColor(),
                      fontSize: 16.sp,
                      fontWeight: FontWeight.w600,
                      letterSpacing: 0,
                      height: 1.2,
                    ),
                  ),
                  SizedBox(height: 20.h,),
                  Text(
                    'Don’t close the app for now! If you interrupt it, you’ll have to start over!',
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: 16.sp,
                      fontWeight: FontWeight.w600,
                      letterSpacing: 0,
                      height: 1.2,
                    ),
                  ),
                  SizedBox(height: 20.h,),
                  Container(
                    color: Colors.white,
                    height: 50.w,
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Image.asset('images/book.png',height: 30.w,width: 30.w,),
                        SizedBox(width: 10.w,),
                        Text(
                          'Health Knowledge Class',
                          style: TextStyle(
                            color: Colors.black,
                            fontSize: 16.sp,
                            fontWeight: FontWeight.w600,
                            letterSpacing: 0,
                            height: 1.2,
                          ),
                        ),
                      ],
                    ),
                  )
                ],
              ),
            )
        )
    );
  }

  void _startTimer() {
    _timer = Timer.periodic(const Duration(seconds: 1), (timer) {
      setState(() {
        print('----------倒计时');
        if(widget.steps == Testing_steps.steps_liquify_countDown){

          if (_liquify_remainingSeconds > 0) {
            _liquify_remainingSeconds--;
          } else {
            _liquify_isFinish = true;
            _timer!.cancel();
            _timer = null;
            setState(() {
              currentStep = 3;
              widget.steps = Testing_steps.steps_4;
            });
            widget.nextAction();
          }
        }else{
          if (_testing_remainingSeconds > 0) {
            _testing_remainingSeconds--;
          } else {
            _testing_isFinish = true;
            _timer!.cancel();

            setState(() {


            });
            widget.nextAction();
          }

        }

      });
    });
  }

  String formatTime(int seconds) {
    int minutes = seconds ~/ 60;
    int remainingSeconds = seconds % 60;
    return '${minutes.toString().padLeft(2, '0')}:${remainingSeconds.toString().padLeft(2, '0')}';
  }

  steps_4_widget(BuildContext context) {
    return Scrollbar(
        thickness: 6,
        radius: const Radius.circular(3),
        interactive: true,
        child: SingleChildScrollView(

            child: Container(
              margin: EdgeInsets.only(left: 20.w,right: 20.w),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  CustomStepIndicator(
                    currentStep: currentStep,
                  ),
                  Text(
                    'PREPARE THE SLIDE FOR TESTING',
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: 20.sp,
                      fontWeight: FontWeight.w600,
                      letterSpacing: 0,
                      height: 1.2,
                    ),
                  ),
                  SizedBox(height: 40.h,),
                  Text(
                    'Pinch the top ‘bulb’ of the pipette, place the tip into your sample and then release the bulb pinch as shown. ',
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: 16.sp,
                      fontWeight: FontWeight.w600,
                      letterSpacing: 0,
                      height: 1.2,
                    ),
                  ),
                  SizedBox(height: 20.h,),
                  Container(
                    margin: EdgeInsets.only(top: 20.h),
                    alignment: Alignment.center,
                    child:  Image.asset('images/cups.png',height: 150.w),
                  ),
                  Container(
                    height: 48.h,
                    width: 335.w,
                    decoration: BoxDecoration(
                      color: Color.fromRGBO(16, 37, 105, 1),
                      borderRadius: BorderRadius.circular(5.h),
                    ),
                    margin: EdgeInsets.only(top: 60.h),
                    child: TextButton(
                      onPressed: () {
                        widget.nextAction();
                      },
                      child: Text(
                        'NEXT',
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: 16.sp,
                          fontWeight: FontWeight.w400,
                        ),
                      ),
                    ),
                  ),
                  Container(
                    height: 48.h,
                    width: 335.w,
                    decoration: BoxDecoration(
                      color: Color.fromRGBO(16, 37, 105, 1),
                      borderRadius: BorderRadius.circular(5.h),
                    ),
                    margin: EdgeInsets.only(top: 20.h),
                    child: TextButton(
                      onPressed: () {
                        widget.backAction();
                      },
                      child: Text(
                        'BACK',
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: 16.sp,
                          fontWeight: FontWeight.w400,
                        ),
                      ),
                    ),
                  ),

                ],
              ),
            )
        )
    );
  }

  steps_5_widget(BuildContext context) {
    return Scrollbar(
        thickness: 6,
        radius: const Radius.circular(3),
        interactive: true,
        child: SingleChildScrollView(

            child: Container(
              margin: EdgeInsets.only(left: 20.w,right: 20.w),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  CustomStepIndicator(
                    currentStep: currentStep,
                  ),
                  Text(
                    'INSERT THE SLIDE',
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: 20.sp,
                      fontWeight: FontWeight.w600,
                      letterSpacing: 0,
                      height: 1.2,
                    ),
                  ),
                  SizedBox(height: 40.h,),
                  Text(
                    'Insert slide FACE UP into device',
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: 16.sp,
                      fontWeight: FontWeight.w600,
                      letterSpacing: 0,
                      height: 1.2,
                    ),
                  ),
                  SizedBox(height: 20.h,),
                  Text(
                    'Firmly push slide again to ensure it is fully inserted.',
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: 16.sp,
                      fontWeight: FontWeight.w600,
                      letterSpacing: 0,
                      height: 1.2,
                    ),
                  ),
                  SizedBox(height: 20.h,),
                  Container(
                    margin: EdgeInsets.only(top: 20.h),
                    alignment: Alignment.center,
                    child:  Image.asset('images/device_3.png',height: 150.w),
                  ),
                  Container(
                    height: 48.h,
                    width: 335.w,
                    decoration: BoxDecoration(
                      color: Color.fromRGBO(16, 37, 105, 1),
                      borderRadius: BorderRadius.circular(5.h),
                    ),
                    margin: EdgeInsets.only(top: 60.h),
                    child: TextButton(
                      onPressed: () {
                        widget.nextAction();
                      },
                      child: Text(
                        'NEXT',
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: 16.sp,
                          fontWeight: FontWeight.w400,
                        ),
                      ),
                    ),
                  ),
                  // Container(
                  //   height: 48.h,
                  //   width: 335.w,
                  //   decoration: BoxDecoration(
                  //     color: Color.fromRGBO(16, 37, 105, 1),
                  //     borderRadius: BorderRadius.circular(5.h),
                  //   ),
                  //   margin: EdgeInsets.only(top: 20.h),
                  //   child: TextButton(
                  //     onPressed: _pickVideo,
                  //     child: Text(
                  //       '相册读取视频',
                  //       style: TextStyle(
                  //         color: Colors.white,
                  //         fontSize: 16.sp,
                  //         fontWeight: FontWeight.w400,
                  //       ),
                  //     ),
                  //   ),
                  // ),
                  Container(
                    height: 48.h,
                    width: 335.w,
                    decoration: BoxDecoration(
                      color: Color.fromRGBO(16, 37, 105, 1),
                      borderRadius: BorderRadius.circular(5.h),
                    ),
                    margin: EdgeInsets.only(top: 20.h),
                    child: TextButton(
                      onPressed: () {
                        widget.backAction();
                      },
                      child: Text(
                        'BACK',
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: 16.sp,
                          fontWeight: FontWeight.w400,
                        ),
                      ),
                    ),
                  ),

                ],
              ),
            )
        )
    );
  }

  testing_in_process_widget(BuildContext context)  {
    return Scrollbar(
        thickness: 6,
        radius: const Radius.circular(3),
        interactive: true,
        child: SingleChildScrollView(

            child: Container(
              margin: EdgeInsets.only(left: 20.w,right: 20.w),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Text(
                    'Testing in Process',
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: 20.sp,
                      fontWeight: FontWeight.w600,
                      letterSpacing: 0,
                      height: 1.2,
                    ),
                  ),
                  SizedBox(height: 20.h,),
                  Container(
                    // color: globalColor(),
                    child: Text(
                      formatTime( _testing_remainingSeconds),
                      style: TextStyle(
                        color: globalColor(),
                        fontSize: 42.sp,
                        fontWeight: FontWeight.w600,
                      ),
                    ),
                  ),
                  SizedBox(height: 20.h,),
                  Text(
                    'Time countdown',
                    style: TextStyle(
                      color: globalColor(),
                      fontSize: 16.sp,
                      fontWeight: FontWeight.w600,
                      letterSpacing: 0,
                      height: 1.2,
                    ),
                  ),
                  SizedBox(height: 20.h,),
                  Text(
                    'Don’t close the app for now! If you interrupt it, you’ll have to start over!',
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: 16.sp,
                      fontWeight: FontWeight.w600,
                      letterSpacing: 0,
                      height: 1.2,
                    ),
                  ),
                  SizedBox(height: 20.h,),
                  Text(
                    'Please DO NOT TOUCH your phone or the device during the testing.',
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: 16.sp,
                      fontWeight: FontWeight.w600,
                      letterSpacing: 0,
                      height: 1.2,
                    ),
                  ),
                  SizedBox(height: 20.h,),
                  Text(
                    'Patience is key for a successful test!',
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: 16.sp,
                      fontWeight: FontWeight.w600,
                      letterSpacing: 0,
                      height: 1.2,
                    ),
                  ),
                  SizedBox(height: 40.h,),
                  Image.asset('images/no_touch.png',height: 200.w,width: 200.w,),

                ],
              ),
            )
        )
    );
  }

  // 从相册选择视频
  Future<void> _pickVideo() async {
    // 调用相册选择视频，可指定最大时长（单位：秒）
    await _picker.pickVideo(source: ImageSource.gallery).then((file) async {
      if (file != null) {
        print('------文件不是空的');

        await basicUsage(file.path);

      }else{
        print('------文件是空的');

      }
    });
  }
  /// 示例：基本使用
  static Future<void> basicUsage(String videoPath) async {
    print('=== 基本使用示例 ===');

    // 1. 创建配置
    final config = Configuration();

    // 2. 创建Motility实例
    final motility = Motility(config: config);

    // 3. 分析视频
    if (videoPath != null) {
      print('开始分析视频: $videoPath');

      final result = await motility.analyze(videoPath);

      if (result != null) {
        print('=====分析完成！');
        print('检测到的精子数量: ${result.numOfDetectedSperms}');
        print('A级精子数量: ${result.numOfALevelSperms}');
        print('B级精子数量: ${result.numOfBLevelSperms}');
        print('C级精子数量: ${result.numOfCLevelSperms}');
        print('活动轨迹数量: ${result.numOfMotileTrajectories}');
        print('平均总位移: ${result.avgTotalDisplacement.toStringAsFixed(2)}');
        print('平均VSL: ${result.avgVsl.toStringAsFixed(2)}');
        print('平均VCL: ${result.avgVcl.toStringAsFixed(2)}');
        print('平均VAP: ${result.avgVap.toStringAsFixed(2)}');
        print('平均ALH: ${result.avgAlh.toStringAsFixed(2)}');
      } else {
        print('=====分析完成！ ==== 分析失败或没有检测到精子');
      }
    } else {
      print('=====未找到示例视频文件');
    }
  }



}

